import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:speed_random/app_styles.dart';
import 'package:speed_random/pages/home/rand_com/rand_com_args.dart';

import 'package:speed_random/widgets/item_rand_list_list.dart';
import 'package:speed_random/widgets/not_data.dart';
import 'package:speed_random/widgets/zdy_confirm.dart';

import 'logic.dart';
import 'state.dart';

class RandListPage extends StatelessWidget {
  RandListPage({Key? key}) : super(key: key);

  final RandListLogic logic = Get.put(RandListLogic());
  final RandListState state = Get.find<RandListLogic>().state;

  // ... 其他代码保持不变 ...

Future<void> _showAddInputDialog(BuildContext context,{index,title}) async {
  final TextEditingController inputController = TextEditingController();
  inputController.text = title??'';
  return showDialog<void>(
    context: context,
    barrierDismissible: false, // 用户不能通过点击对话框外部关闭对话框
    builder: (BuildContext context) {
      return AlertDialog(
        title: Text(index!=null?'修改列表'.tr:'添加列表'.tr),
        content: SingleChildScrollView(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              // ... 其他代码保持不变 ...

              TextField(
                controller: inputController,

                decoration: InputDecoration(
                  hintText: '请输入列表项名称'.tr,
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(12), // 设置圆角半径
                    borderSide: BorderSide(
                      color: Colors.grey.withOpacity(0.5), // 边框颜色
                      width: 1.0, // 边框宽度
                    ),
                  ),
                  enabledBorder: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(12),
                    borderSide: BorderSide(
                      color: Colors.grey.withOpacity(0.5),
                      width: 1.0,
                    ),
                  ),
                  focusedBorder: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(12),
                    borderSide: BorderSide(
                      color: Colors.blue.withOpacity(0.8), // 聚焦时边框颜色
                      width: 2.0,
                    ),
                  ),
                  contentPadding: EdgeInsets.symmetric(horizontal: 16, vertical: 12), // 内边距
                ),
                maxLines: 5,
                minLines: 1,
              ),

// ... 其他代码保持不变 ...
            ],
          ),
        ),
        actions: <Widget>[
          TextButton(
            child: Text('取消'.tr),
            onPressed: () {
              Navigator.of(context).pop();
            },
          ),
          TextButton(
            child: Text('确定'.tr),
            onPressed: () {
              if (inputController.text.trim().isNotEmpty) {
                logic.addCustomList(index,title: inputController.text);
                Navigator.of(context).pop();
              }
            },
          ),
        ],
      );
    },
  );
}

// ... 其他代码保持不变 ...


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title:  Obx(()=>Text(state.randComArgs.value.title.tr+'(${state.historyNumbers.value.length})')),
       // backgroundColor: AppStyles.colorAppBarbg,
        actions: [
          IconButton(
            icon: const Icon(Icons.add),
            onPressed: () {
              _showAddInputDialog(context);
            },
          ),
        ],
      ),
      body:Obx(() =>state.historyNumbers.value.length==0? notData(): ListView.builder(
        itemCount: state.historyNumbers.value.length,
        padding: const EdgeInsets.all(5),
        itemBuilder: (context, index) {
          final indext = state.historyNumbers.value.length - 1 - index;
          final numbers = state.historyNumbers.value[indext];
          return itemRandListList(
            numbers: numbers,
            onTopPressed: (){
              if(numbers["list"].length<1) {
                  EasyLoading.showError('列表项为空不能使用，请先添加列表项'.tr);
                  Get.toNamed('/rand_list_list',arguments: {
                    'list': numbers["list"],
                    'title': numbers["title"],
                    'id':  numbers["id"],
                    'index': indext,
                  });
                  return;
              }
              Get.toNamed('/rand_com', arguments: RandComArgs(
                title: numbers["title"],
                list: numbers["list"],
                cachekey:state.randComArgs.value.cachekey+numbers["id"],
                colorPagebg: state.randComArgs.value.colorPagebg, // 添加背景色参数
                colorPageText: state.randComArgs.value.colorPageText, // 添加文字颜色参数
                settingkeys: state.randComArgs.value.settingkeys,
              ).toMap());


            },
            onEditPressed: (){
              Get.toNamed('/rand_list_list',arguments: {
                'list': numbers["list"],
                'title': numbers["title"],
                'id':  numbers["id"],
                'index': indext,
              });
            },
            onEditTitlePressed: (){
              _showAddInputDialog(context,index: indext,title: numbers["title"]);
            },
            onDelePressed: () async{
              zdyConfirm(context: context, title: "提示".tr, content: "是否确定删除".tr,confirmText: "确定".tr,cancelText: "取消".tr, onOKPressed: (){
                logic.removeCustomList(indext);
              }, onCancelPressed: (){

              });

          },
          );
        },
      )),
    );
  }
}
